package com.txby.common.utils.util;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.txby.core.entity.DataSourceCnf;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.junit.Test;  
  
public class EncryptUtils {  
    // 密钥  
	private static  EncryptUtils  eu ;  
    private static  String KEY = "1234567890abcdefghijklmn";  
    /**
     * 前缀
     */
    private static  String  prefix = "ENC(";  
    /**
     * 后缀
     */
    private static  String suffix = ")";  
      
/*    public static void main(String[] args) {  
        String ciphertext1 = encrypt("123456"); // Wu11fsC0gpgSET5aU8GXUA==  
//        String ciphertext2 = encrypt("abcdefg"); // ESXlHsVk2YM7mGcHy2ccGg==  
        System.out.println(ciphertext1);  
//        System.out.println(ciphertext2);  
          
        String text1 = decrypt("ftk/mllBGMZS+Dw/VlPgPg==");  
//        String text2 = decrypt(ciphertext2);  
        System.out.println(text1);               // abcdefg  
//        System.out.println(text2);               // abcdefg  
    }*/ 
    @Test
    public void testsPass(){
    	String text="jdbc:mysql://192.168.2.71:3306/dplus?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8";
    	String str=EncryptUtils.encrypt(text);
    	System.out.println(str);
    	String  Str="ENC("+str+")";
    	System.out.println(Str);
    	System.out.println(decryptByENC(Str));
    	

    }
    
    /**
     * 返回ENC加密后的字符
    * @Title: encryptENC 
    * @Description: TODO(这里用一句话描述这个方法的作用) 
    * @param @param text
    * @param @return    设定文件 
    * @return String    返回类型 
    * @throws
     */
    public static String encryptENC(String text){
    	if(text!=null) {
    		return  "ENC("+EncryptUtils.encrypt(text)+")";
    	}else {
    		return "";
    	}
    }
    
	/**
	 * 	Map m=new HashMap();
		m.put("ip", ip);
		m.put("port", port);
		m.put("dbname", dbname);
		return m;
	* @Title:  解析jdbcurl数据连接地址信息为map
	* @Description: TODO(这里用一句话描述这个方法的作用) 
	* @param @param jdbcurl
	* @param @return    设定文件 
	* @return Map    返回类型 
	* @throws
	 */
	public static Map<String,String> getJdbcInfoByJdbcUrl(String jdbcurl){
//		jdbcurl=jdbcurl.toLowerCase();
		Map m=new HashMap();
		String ip="";
		String port="";
		String dbname="";
		if(jdbcurl.startsWith("jdbc:sqlserver")){
			String ipprot=jdbcurl.substring(jdbcurl.indexOf("//")+2,jdbcurl.indexOf(";"));
			dbname=jdbcurl.substring(jdbcurl.lastIndexOf("=")+1);
			ip=ipprot.split(":")[0];
			port=ipprot.split(":")[1];
		}else if(jdbcurl.startsWith("jdbc:mysql")){
			String ipprot=jdbcurl.substring(jdbcurl.indexOf("//")+2,jdbcurl.lastIndexOf("/"));
			dbname=jdbcurl.substring(jdbcurl.lastIndexOf("/")+1,jdbcurl.lastIndexOf("?"));
			ip=ipprot.split(":")[0];
			port=ipprot.split(":")[1];
		} else if(jdbcurl.startsWith("jdbc:oracle:thin")){
			String ipprot=jdbcurl.substring(jdbcurl.indexOf("@")+1,jdbcurl.lastIndexOf(":"));
			dbname=jdbcurl.substring(jdbcurl.lastIndexOf(":")+1);
			ip=ipprot.split(":")[0];
			port=ipprot.split(":")[1];
	    }
	
		m.put("ip", ip);
		m.put("port", port);
		m.put("dbname", dbname);
		return m;
	}
	/**
	 * 拼接并加密HostUrl
	 *
	 * @param dataSourceCnf 数据源数据
	 * @return String 加密之后的dataHostUrl数据
	 */
	public static String getENCHostUrl(DataSourceCnf dataSourceCnf) {
		return encryptENC(getHostUrl(dataSourceCnf));
	}

	/**
	 * 拼接HostUrl
	 *
	 * @param dataSourceCnf 数据源数据
	 * @return String
	 */
	public static String getHostUrl(DataSourceCnf dataSourceCnf) {
		String hostUrl = null;
		switch (dataSourceCnf.getDataType()) {
			case 0:
				//jdbc:mysql://192.168.2.71:3306/txby-hisjob
				hostUrl = "jdbc:mysql://" + dataSourceCnf.getDataHostUrl().replaceAll("\\s+","") + ":" + dataSourceCnf.getPort().replaceAll("\\s+","")  + "/" + dataSourceCnf.getDataBaseName().replaceAll("\\s+","")  + "?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false";
				break;
			case 1:
				//jdbc:sqlserver://192.168.2.72:1433;databaseName=txby-platform
				hostUrl = "jdbc:sqlserver://" + dataSourceCnf.getDataHostUrl().replaceAll("\\s+","")  + ":" + dataSourceCnf.getPort().replaceAll("\\s+","")  + ";databaseName=" + dataSourceCnf.getDataBaseName().replaceAll("\\s+","") ;
				break;
			case 2:
				//jdbc:oracle:thin:@172.25.8.135:1521:zhlis1
				hostUrl = "jdbc:oracle:thin:@" + dataSourceCnf.getDataHostUrl().replaceAll("\\s+","")  + ":" + dataSourceCnf.getPort().replaceAll("\\s+","")  + ":" + dataSourceCnf.getDataBaseName().replaceAll("\\s+","") ;
				break;
		}
		return hostUrl;
	}
	
  /*  private EncryptUtils(){}
    public synchronized static EncryptUtils getInstance(){
        if(eu == null){
        	eu= new EncryptUtils();
        }
        return eu;
    } 
    public synchronized static EncryptUtils getInstance(String passwordKey){
    	KEY=passwordKey;
        eu= new EncryptUtils();
    	return eu;
    } */
      
    /** 
     * 加密 
     * @param text 明文 
     * @return     密文 
     */  
    public static String encrypt(String text) {  
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();  
        encryptor.setPassword(KEY);  
        return encryptor.encrypt(text);  
    }  
      
    /** 
     * 解密 
     * @param ciphertext 密文 
     * @return           明文 
     */  
    public static String decrypt(String ciphertext) {  
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
    	ciphertext=replaceStr(ciphertext);
    	encryptor.setPassword(KEY);  
    	return encryptor.decrypt(ciphertext);  
    }  
    public static String decrypt(String ciphertext,String KEY) {  
    	StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
    	ciphertext=replaceStr(ciphertext);
    	encryptor.setPassword(KEY);  
    	return encryptor.decrypt(ciphertext);  
    }  
    public static String decryptByENC(String ciphertext) {  
    	StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
    	if(isPass(ciphertext)){
//    		ciphertext=replaceStr(ciphertext);
    		ciphertext= ciphertext.replaceAll ("[\\s\\S]*(?<=ENC\\()([\\s\\S]+)(?=\\))[\\s\\S]*", "$1");
    		encryptor.setPassword(KEY);  
    		return encryptor.decrypt(ciphertext);  
    	}else{
    		
    		return ciphertext;
    	}
    	
    }  
    public static String decryptByENC(String ciphertext,String KEY) {  
    	StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
    	if(isPass(ciphertext)){
    		ciphertext=replaceStr(ciphertext);
    		encryptor.setPassword(KEY);  
    		return encryptor.decrypt(ciphertext);  
    	}else{
    		
    		return ciphertext;
    	}
    	
    }  
    public static boolean  isPass(String ciphertext) {  
    	
    	return ciphertext.contains(prefix)&&ciphertext.endsWith(suffix);  
//    	return ciphertext.startsWith(prefix)&&ciphertext.endsWith(suffix);  
    }  
    
    private  static String replaceStr(String str){
//        Pattern pattern = Pattern.compile("^,+|,+$");
//        Pattern pattern = Pattern.compile("^ENC\\(+|\\)+$");
        Pattern pattern = Pattern.compile("^ENC\\(+|\\)+$");
//        replaceAll("/\\*start\\*/(.*\\s+)*/\\*end\\*/","");
        Matcher matcher = pattern.matcher(str);
        return matcher.replaceAll("");
    }
/*    public static void main(String[] args) {
 
//		System.out.println(replaceStr("ENC(fdfdf)"));
		System.out.println(s);
		String regex = "[\\s\\S]*(?<=\\/\\*start\\*\\/)([\\s\\S]+)(?=\\/\\*end\\*\\/)[\\s\\S]*";
		  regex = "[\\s\\S]*(?<=ENC\\()([\\s\\S]+)(?=\\))[\\s\\S]*";
		  regex = "[\\s\\S]*(?<=ENC\\()([\\s\\S]+)(?=\\))[\\s\\S]*";
		System.out.println (s.replaceAll (regex, "$1"));
	}*/
}  